package com.xoa.plugin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xoa.dao.work.WorkMapper;
import com.xoa.model.nccloud.NCCInterfaceLog;
import com.xoa.model.workflow.TriggerPlugIn;
import com.xoa.plugin.archives.ct.oaws.WorkFlowAdministrativeTrigger;
import com.xoa.plugin.ct.CTAdjustWagesTrigger;
import com.xoa.plugin.ct.CTLocationWagesTrigger;
import com.xoa.plugin.jf.JFAdjustWagesTrigger;
import com.xoa.plugin.jf.JFLocationWagesTrigger;
import com.xoa.service.nccloud.INCCInterFaceLogService;
import com.xoa.service.nccloud.INCCPsndocService;
import com.xoa.service.nccloud.INCCViewService;
import com.xoa.service.nccloud.INCCloudService;
import com.xoa.util.ToJson;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * flow_plugin 流程审批结束触发器
 * 最后一个节点触发
 */
@Service
public class WorkFlowEndTrigger {

    //表数据
    private TriggerPlugIn tri = null;
    @Autowired
    private INCCloudService nccloudService;
    @Autowired
    private INCCInterFaceLogService logService;

    @Autowired
    private INCCPsndocService psndocService;
    @Autowired
    private WorkMapper workMapper;
    //NCC 视图操作类
    @Autowired
    private INCCViewService viewService;
    // @Autowired
    WorkFlowAdministrativeTrigger workFlowAdministrativeTrigger;


    public ToJson doRun(TriggerPlugIn tri, HttpServletRequest httpServletRequest) throws Exception {

        this.tri = tri;
        String run_id = tri.getRunId();
        String tableName = tri.getFlowId();
        ToJson toJson = new ToJson();


        //===============================城投
        try {
            /**
             * 人员内部调动
             */
            if (StringUtils.isNotBlank(run_id) && tableName.equals("292")) {
                String pk = null;
                JSONObject[] jsonObjectRs = null;


                try {
                    //=====1、流水号查询
                    Map<String, Object> map = new HashedMap();
                    map.put("tableName", "flow_data_292");
                    map.put("runId", run_id);
                    Map<String, Object> rsmap = workMapper.select(map);


                    //=====2、提交动作接口
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();
                    // 提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    jsonObjectRs = this.billsAction(pk, billtype, "", "Y", "APPROVE");

                    //判断请求是否错误
                    if (jsonObjectRs[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "城投个人内部调动310runid:" + run_id + "最后审批动作true", jsonObjectRs[1].toJSONString(), "1");
                        toJson.setFlag(0);
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "城投个人内部调动310runid:" + run_id + "最后审批动作false", jsonObjectRs[1].toJSONString(), "0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs[1].getString("message"));
                        toJson.setFlag(0);
                    }
                } catch (Exception e) {
                    this.saveLog("", "", "城投个人内部调动310runid:" + run_id + "最后审批动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());

                }

                /**
                 * 个离职表单接口
                 */
            } else if (tableName.equals("255")) {
                String pk = null;
                JSONObject[] jsonObjectRs = null;


                try {
                    //=====1、流水号查询
                    Map<String, Object> map = new HashedMap();
                    map.put("tableName", "flow_data_255");
                    map.put("runId", run_id);
                    Map<String, Object> rsmap = workMapper.select(map);


                    //=====2、提交动作接口
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();
                    // 提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    jsonObjectRs = this.billsAction(pk, billtype, "", "Y", "APPROVE");


                    //判断请求是否错误
                    if (jsonObjectRs[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "城投个人离职255runid:" + run_id + "最后审批动作true", jsonObjectRs[1].toJSONString(), "1");
                        toJson.setFlag(0);
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "城投个人离职255runid:" + run_id + "最后审批动作false", jsonObjectRs[1].toJSONString(), "0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs[1].getString("message"));

                    }
                } catch (Exception e) {
                    this.saveLog("", "", "城投个人离职255runid:" + run_id + "最后审批动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());

                }

                /**
                 * 人事发起转正
                 */
            }else if (tableName.equals("311")) {
                String pk = null;
                JSONObject[] jsonObjectRs = null;


                try {
                    //=====1、流水号查询
                    Map<String, Object> map = new HashedMap();
                    map.put("tableName", "flow_data_311");
                    map.put("runId", run_id);
                    Map<String, Object> rsmap = workMapper.select(map);


                    //=====2、提交动作接口
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();
                    // 提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    jsonObjectRs = this.billsAction(pk, billtype, "", "Y", "APPROVE");


                    //判断请求是否错误
                    if (jsonObjectRs[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "城投人事发起转正311runid:" + run_id + "最后审批动作true", jsonObjectRs[1].toJSONString(), "1");
                        toJson.setFlag(0);
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "城投人事发起转正311runid:" + run_id + "最后审批动作false", jsonObjectRs[1].toJSONString(), "0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs[1].getString("message"));

                    }
                } catch (Exception e) {
                    this.saveLog("", "", "城投人事发起转正311runid:" + run_id + "最后审批动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());

                }

                /**
                 * 人事审批离职
                 */
            }else if (tableName.equals("303")) {
                String pk = null;
                JSONObject[] jsonObjectRs = null;


                try {
                    //=====1、流水号查询
                    Map<String, Object> map = new HashedMap();
                    map.put("tableName", "flow_data_303");
                    map.put("runId", run_id);
                    Map<String, Object> rsmap = workMapper.select(map);


                    //=====2、提交动作接口
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();
                    // 提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    jsonObjectRs = this.billsAction(pk, billtype, "", "Y", "APPROVE");


                    //判断请求是否错误
                    if (jsonObjectRs[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "城投人事审批离职303runid:" + run_id + "最后审批动作true", jsonObjectRs[1].toJSONString(), "1");
                        toJson.setFlag(0);
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "城投人事审批离职303runid:" + run_id + "最后审批动作false", jsonObjectRs[1].toJSONString(), "0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs[1].getString("message"));

                    }
                } catch (Exception e) {
                    this.saveLog("", "", "城投人事审批离职303runid:" + run_id + "最后审批动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());

                }

                /**
                 * 调资
                 */
            } else if (tableName.equals("284")) {

                String reqeustBody = null;
                try {
                    //=====1、请求nc接口保存
                    // 封装NC接口表所需json数据
                    reqeustBody = CTAdjustWagesTrigger.onversioncDto284(tri.getModify(), run_id, viewService, psndocService);
                    // 请求发送保存
                    String result = nccloudService.sendApi(nccloudService.psndocWadocManageUrl, reqeustBody);
                    //转换json对象
                    JSONObject rsjson = JSON.parseObject(result);
                    //判断请求是否错误
                    if (rsjson.getIntValue("code") == 0) {

                        //请求发送保存json日志保存
                        this.saveLog("", reqeustBody, "城投调资信息284run_id:" + run_id + "最后提交人员保存true", result, "1");
                        toJson.setFlag(0);
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog("", reqeustBody, "城投调资信息284run_id:" + run_id + "最后提交人员保存false", result, "0");
                        toJson.setFlag(1);
                        toJson.setMsg(rsjson.getString("message"));

                    }
                } catch (Exception e) {
                    this.saveLog("", "", "城投调资信息284run_id:" + run_id + "最后最后保存动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());

                }

                /**
                 * 定资
                 */
            } else if (tableName.equals("290")) {


                try {
                    //=====1、请求nc接口保存
                    // 封装NC接口表所需json数据
                    String reqeustBody = CTLocationWagesTrigger.onversioncDto290(tri.getModify(), run_id, viewService, psndocService);
                    // 请求发送保存
                    String result = nccloudService.sendApi(nccloudService.psndocWadocManageUrl, reqeustBody);
                    //转换json对象
                    JSONObject rsjson = JSON.parseObject(result);
                    //判断请求是否错误
                    if (rsjson.getIntValue("code") == 0) {

                        //请求发送保存json日志保存
                        this.saveLog("", reqeustBody, "城投定资信息290run_id:" + run_id + "最后提交人员保存true", result, "1");
                        toJson.setFlag(0);
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog("", reqeustBody, "城投定资信息290run_id:" + run_id + "最后提交人员保存false", result, "0");
                        toJson.setFlag(1);
                        toJson.setMsg(rsjson.getString("message"));

                    }
                } catch (Exception e) {
                    this.saveLog("", "", "城投定资信息290run_id:" + run_id + "最后保存动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());

                }


                /**
                 * 转正
                 */
            } else if (tableName.equals("232")) {
                String pk = null;
                JSONObject[] jsonObjectRs = null;


                try {
                    //=====1、流水号查询
                    Map<String, Object> map = new HashedMap();
                    map.put("tableName", "flow_data_232");
                    map.put("runId", run_id);
                    Map<String, Object> rsmap = workMapper.select(map);


                    //=====2、提交动作接口
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();
                    // 提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    jsonObjectRs = this.billsAction(pk, billtype, "", "Y", "APPROVE");


                    //判断请求是否错误
                    if (jsonObjectRs[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "城投转正232runid:" + run_id + "最后审批动作true", jsonObjectRs[1].toJSONString(), "1");
                        toJson.setFlag(0);
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "城投转正232runid:" + run_id + "最后审批动作false", jsonObjectRs[1].toJSONString(), "0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs[1].getString("message"));

                    }
                } catch (Exception e) {
                    this.saveLog("", "", "城投转正232runid:" + run_id + "最后审批动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());

                }
//==================================================档案接口推送=============================================


                //行政发文
            }else if (tableName.equals("205")) {

                try {
                    ToJson json = workFlowAdministrativeTrigger.doRun(tri, httpServletRequest);
                    if(json.isFlag()){
                        this.saveLog("", "", "城投行政发文205runid:" + run_id + "最后推送档案true", "", "1");

                        toJson.setFlag(0);
                    }else {
                        throw new RuntimeException(json.getMsg());
                    }


                } catch (Exception e) {
                    this.saveLog("", "", "城投行政发文205runid:" + run_id + "最后推送档案false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg("档案数据归档推送false"+e.getMessage());

                }

            }
                //====================================建发

                //离职
             else if (tableName.equals("308")) {
                String pk = null;
                JSONObject[] jsonObjectRs = null;


                try {
                    //=====1、流水号查询
                    Map<String, Object> map = new HashedMap();
                    map.put("tableName", "flow_data_308");
                    map.put("runId", run_id);
                    Map<String, Object> rsmap = workMapper.select(map);


                    //=====2、提交动作接口
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();
                    // 提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    jsonObjectRs = this.billsAction(pk, billtype, "", "Y", "APPROVE");


                    //判断请求是否错误
                    if (jsonObjectRs[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "建发个人离职308runid:" + run_id + "最后审批动作true", jsonObjectRs[1].toJSONString(), "1");
                        toJson.setFlag(0);
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "建发个人离职308runid:" + run_id + "最后审批动作false", jsonObjectRs[1].toJSONString(), "0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs[1].getString("message"));

                    }
                } catch (Exception e) {
                    this.saveLog("", "", "建发个人离职308runid:" + run_id + "最后审批动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());

                }
                /**
                 * 转正
                 */
            } else if (tableName.equals("313")) {
                String pk = null;
                JSONObject[] jsonObjectRs = null;


                try {
                    //=====1、流水号查询
                    Map<String, Object> map = new HashedMap();
                    map.put("tableName", "flow_data_313");
                    map.put("runId", run_id);
                    Map<String, Object> rsmap = workMapper.select(map);


                    //=====2、提交动作接口
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();
                    // 提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    jsonObjectRs = this.billsAction(pk, billtype, "", "Y", "APPROVE");


                    //判断请求是否错误
                    if (jsonObjectRs[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "建发个人转正313runid:" + run_id + "最后审批动作true", jsonObjectRs[1].toJSONString(), "1");
                        toJson.setFlag(0);
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "建发个人转正313runid:" + run_id + "最后审批动作false", jsonObjectRs[1].toJSONString(), "0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs[1].getString("message"));

                    }
                } catch (Exception e) {
                    this.saveLog("", "", "建发个人转正313runid:" + run_id + "最后审批动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());

                }
                /**
                 * 人员内部调动
                 */
            }else if (StringUtils.isNotBlank(run_id) && tableName.equals("310")) {
                String pk = null;
                JSONObject[] jsonObjectRs = null;


                try {
                    //=====1、流水号查询
                    Map<String, Object> map = new HashedMap();
                    map.put("tableName", "flow_data_310");
                    map.put("runId", run_id);
                    Map<String, Object> rsmap = workMapper.select(map);


                    //=====2、提交动作接口
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();
                    // 提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    jsonObjectRs = this.billsAction(pk, billtype, "", "Y", "APPROVE");

                    //判断请求是否错误
                    if (jsonObjectRs[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "建发个人内部调动310runid:" + run_id + "最后审批动作true", jsonObjectRs[1].toJSONString(), "1");
                        toJson.setFlag(0);
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "建发个人内部调动310runid:" + run_id + "最后审批动作false", jsonObjectRs[1].toJSONString(), "0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs[1].getString("message"));
                        toJson.setFlag(0);
                    }
                } catch (Exception e) {
                    this.saveLog("", "", "建发个人内部调动310runid:" + run_id + "最后审批动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());

                }
                /**
                 * 定资
                 */
            } else if (tableName.equals("307")) {


                try {
                    //=====1、请求nc接口保存
                    // 封装NC接口表所需json数据
                    String reqeustBody = JFLocationWagesTrigger.onversioncDto290(tri.getModify(), run_id, viewService, psndocService);
                    // 请求发送保存
                    String result = nccloudService.sendApi(nccloudService.psndocWadocManageUrl, reqeustBody);
                    //转换json对象
                    JSONObject rsjson = JSON.parseObject(result);
                    //判断请求是否错误
                    if (rsjson.getIntValue("code") == 0) {

                        //请求发送保存json日志保存
                        this.saveLog("", reqeustBody, "建发定资信息305run_id:" + run_id + "最后提交人员保存true", result, "1");
                        toJson.setFlag(0);
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog("", reqeustBody, "建发定资信息305run_id:" + run_id + "最后提交人员保存false", result, "0");
                        toJson.setFlag(1);
                        toJson.setMsg(rsjson.getString("message"));

                    }
                } catch (Exception e) {
                    this.saveLog("", "", "建发定资信息305run_id:" + run_id + "最后保存动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());

                }/**
                 * 调资
                 */
            } else if (tableName.equals("309")) {

                String reqeustBody = null;
                try {
                    //=====1、请求nc接口保存
                    // 封装NC接口表所需json数据
                    reqeustBody = JFAdjustWagesTrigger.onversioncDto284(tri.getModify(), run_id, viewService, psndocService);
                    // 请求发送保存
                    String result = nccloudService.sendApi(nccloudService.psndocWadocManageUrl, reqeustBody);
                    //转换json对象
                    JSONObject rsjson = JSON.parseObject(result);
                    //判断请求是否错误
                    if (rsjson.getIntValue("code") == 0) {

                        //请求发送保存json日志保存
                        this.saveLog("", reqeustBody, "建发调资信息301run_id:" + run_id + "最后提交人员保存true", result, "1");
                        toJson.setFlag(0);
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog("", reqeustBody, "建发调资信息301run_id:" + run_id + "最后提交人员保存false", result, "0");
                        toJson.setFlag(1);
                        toJson.setMsg(rsjson.getString("message"));

                    }
                } catch (Exception e) {
                    this.saveLog("", "", "建发调资信息301run_id:" + run_id + "最后最后保存动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());

                }

                //================================================省二建=============================
                /**
                 * 个人离职
                 */

            }else if (tableName.equals("314")) {
                String pk = null;
                JSONObject[] jsonObjectRs = null;


                try {
                    //=====1、流水号查询
                    Map<String, Object> map = new HashedMap();
                    map.put("tableName", "flow_data_314");
                    map.put("runId", run_id);
                    Map<String, Object> rsmap = workMapper.select(map);


                    //=====2、提交动作接口
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();
                    // 提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    jsonObjectRs = this.billsAction(pk, billtype, "", "Y", "APPROVE");


                    //判断请求是否错误
                    if (jsonObjectRs[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "省二建个人离职314runid:" + run_id + "最后审批动作true", jsonObjectRs[1].toJSONString(), "1");
                        toJson.setFlag(0);
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "省二建个人离职314runid:" + run_id + "最后审批动作false", jsonObjectRs[1].toJSONString(), "0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs[1].getString("message"));

                    }
                } catch (Exception e) {
                    this.saveLog("", "", "省二建个人离职314runid:" + run_id + "最后审批动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());

                }

                /**
                 * 转正
                 */
            } else if (tableName.equals("315")) {
                String pk = null;
                JSONObject[] jsonObjectRs = null;


                try {
                    //=====1、流水号查询
                    Map<String, Object> map = new HashedMap();
                    map.put("tableName", "flow_data_315");
                    map.put("runId", run_id);
                    Map<String, Object> rsmap = workMapper.select(map);


                    //=====2、提交动作接口
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();
                    // 提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    jsonObjectRs = this.billsAction(pk, billtype, "", "Y", "APPROVE");


                    //判断请求是否错误
                    if (jsonObjectRs[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "省二建个人转正315runid:" + run_id + "最后审批动作true", jsonObjectRs[1].toJSONString(), "1");
                        toJson.setFlag(0);
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs[0].toJSONString(), "省二建个人转正315runid:" + run_id + "最后审批动作false", jsonObjectRs[1].toJSONString(), "0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs[1].getString("message"));

                    }
                } catch (Exception e) {
                    this.saveLog("", "", "省二建个人转正315runid:" + run_id + "最后审批动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());

                }

            }else {
                toJson.setFlag(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //请求发送保存json日志保存
            this.saveLog("", "", "最后流程调用审批接口失败", e.getMessage(), "0");
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }


        return toJson;
    }


    /**
     * 日志保存
     *
     * @param pk
     * @param reqeustBody
     * @param name
     * @param result
     * @throws Exception
     */
    private void saveLog(String pk, String reqeustBody, String name, String result, String state) throws Exception {
        NCCInterfaceLog ncclog = new NCCInterfaceLog();
        ncclog.setBusinessNo(pk);
        ncclog.setContent(reqeustBody);
        ncclog.setName(name);
        ncclog.setResult(result);
        ncclog.setState(state);
        ncclog.setDisposeState("0");
        logService.save(ncclog);
    }


    /**
     * 单据动作
     *
     * @param bills
     * @return
     * @throws Exception
     */
    public JSONObject[] billsAction(String... bills) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pk", bills[0]);//单据主键
        jsonObject.put("billType", bills[1]);//单据类型编码
        jsonObject.put("checkNote", bills[2]);//批语
        jsonObject.put("approveResult", bills[3]);//审批结果
        jsonObject.put("action", bills[4]);//单据动作名称
        //请求发送动作
        String rs = nccloudService.sendApi(nccloudService.billactionUrl, jsonObject.toJSONString());

        return new JSONObject[]{jsonObject, JSON.parseObject(rs)};
    }

    /**
     * 保存单据信息
     *
     * @param pk
     * @param billsType
     * @param run_id
     * @throws Exception
     */
    public void saveBills(String pk, String billsType, String run_id) throws Exception {
        // 保存主键的动作
        List<String> key = new ArrayList();
        key.add("pk_hi_stapply='" + pk + "'");
        key.add("billsType='" + billsType + "'");
        Map<String, Object> param = new HashMap();
        param.put("tableName", "flow_data_284");
        param.put("keys", key);
        param.put("runId", run_id);
        workMapper.update(param);
    }
}
